(function(){
  ajax({
   url:"http://localhost:3000/user_productRouter",//user是服务器中挂载""中的路径名称
   type:"get",
   //data:,
   dataType:"json" //自动JSON.parse()
 }).then(function(result){
   console.log(result)
   if(result.length>0){
      var user_table=document.getElementById("user_table")
      var thead=` <thead>
                    <tr>
                        <th>产品名称</th>
                        <th>买入金额</th>
                        <th>现有总值</th>
                        <th>到期时间</th>
                        <th>累计收益</th>
                        <th>操作</th>
                    </tr>
                  </thead>`
      user_table.innerHTML=thead            
      var tbody=document.createElement("tbody")
      for(var item of result){
        var tr=document.createElement('tr');
        var date=new Date(item.product_date);
        var today=new Date();
        var ddd=parseInt((today-date)/(1000*60*60*24));
        if(ddd>=item.profitDate){
          ddd=item.profitDate
        }
        var profit=(item.buyAccount*item.hrate/100*ddd/365).toFixed(2)
        //console.log(profit)
        var y=date.getFullYear();
        var m=date.getMonth()+1;
        var d=date.getDate();
        tr.innerHTML=`<tr>
                        <td>${item.productName}</td>
                        <td>${item.buyAccount}元</td>
                        <td><span class="nowMoney">${item.buyAccount-(-profit)}</span>元</td>
                        <td>${y}-${m}-${d}</td>
                        <td>${profit}元</td>
                        <td><a href="javascript:;" data-id="${item.userProductId}" class="sellIt">卖出</a></td>
                      </tr>`
         tbody.appendChild(tr)      
      }
      user_table.appendChild(tbody);
      console.log(user_table);

      // sell
      var sells=document.getElementsByClassName("sellIt")
      for(var sell of sells){
        sell.onclick=function(e){
          var userProductId=e.target.getAttribute("data-id")
          var nowMoney=document.getElementsByClassName("nowMoney")
          var buyAccount=nowMoney[nowMoney.length-1].innerHTML
          ajax({
            url:"http://localhost:3000/user_productRouter/sell?userProductId="+userProductId+"&buyAccount="+buyAccount,
            type:"get",
            //data:,
            dataType:"json" //自动JSON.parse()
          }).then(function(result){
            if(result.code==1){
              alert(result.msg)
              window.location.reload();
            }
          })
        }
      }
   }else{
     var p=document.createElement('p');
     var part=document.getElementById('my_account_right')
     p.innerHTML='您还未购买任何产品';
    part.appendChild(p)
   }
   })
 })()